package com.qf.service.impl;

import com.qf.dao.CartDao;
import com.qf.dao.impl.CartDaoImpl;
import com.qf.pojo.TbCart;
import com.qf.pojo.TbGoods;
import com.qf.service.CartService;
import com.qf.service.GoodsService;

import java.util.List;

/*
 *@Description:
 *@author:刘哥
 *@Date:86180 2022/8/17 11:52
 */
public class CartServiceImpl implements CartService {
    private CartDao cartDao=new CartDaoImpl();
    @Override
    public List<TbCart> selectAll(Long id) {
        List<TbCart> cartList = cartDao.findById(id);
        if (cartList != null) {
            for (TbCart tbCart : cartList) {
                GoodsService goodsService=new GoodsServiceImpl();
                TbGoods goodsList=goodsService.findByGid(tbCart.getPid());
                tbCart.setGoodsList(goodsList);
            }
        }

        return cartList;

    }

    @Override
    public void selectCart(Long id, Long pid, int num) {
        TbCart tbCarts = cartDao.selectCart(id, pid);
        //       有订单de情况下
        if (tbCarts != null) {
            tbCarts.setNum(tbCarts.getNum()+num);
            int updateNum = cartDao.updateNum(id, pid, num);
        }else{
            cartDao.insetCart(id,pid,num);
        }

    }

    @Override
    public void deleteCart(Long id, long pid) {
        cartDao.deleteCart(id,pid);
    }

    @Override
    public void clearCart(Long uid) {
        cartDao.clearCart(uid);
    }
}
